using System;
using System.Collections.Generic;
using System.Linq;
using Blackbird.Common.Exceptions;

namespace Blackbird.Core.Entities.Sprites
{
    [Serializable]
    public class FramesetList : List<Frameset>
    {
        private Frameset _selectedFrameset;

        private int _selectedFramesetIndex;
        public string Name { get; set; }

        public Frameset SelectedFrameset
        {
            get { return _selectedFrameset; }
        }

        public int SelectedFramesetIndex
        {
            get { return _selectedFramesetIndex; }
            set { SetSelectedFrameset(value); }
        }

        public string SelectedFramesetName
        {
            get { return _selectedFrameset.Name; }
            set { SetSelectedFrameset(value); }
        }

        public Frameset this[string name]
        {
            get { return this.SingleOrDefault(fs => fs.Name == name); }
        }

        private void SetSelectedFrameset(string value)
        {
            int index = FindIndex(fs => fs.Name == value);
            if (index != -1)
            {
                _selectedFrameset = this[index];
                _selectedFramesetIndex = index;
            }
            else
                InvalidFramesetException.Throw();
        }

        private void SetSelectedFrameset(int value)
        {
            if ((value < 0) || (value >= Count))
                InvalidFramesetException.Throw();
            else
            {
                _selectedFrameset = this[value];
                _selectedFramesetIndex = value;
            }
        }
    }
}